package com.yanqu.road.dao.impl.activity.unionchest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.unionChest.CrossUnionChestBoxData;
import com.yanqu.road.entity.activity.unionChest.CrossUnionChestUserData;
import com.yanqu.road.entity.activity.unionChest.UnionChestBoxInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionChestUserDaoImpl extends TempDataObjectDao<CrossUnionChestUserData> {
    public static String getCrossUnionChestUserDataSql = "select * from `t_c_unionchest_user` where `activityId`=?";

    public Map<Long, CrossUnionChestUserData> getCrossUnionChestUserDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, CrossUnionChestUserData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossUnionChestUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossUnionChestUserData userData = parse(rs);
                dataMap.put(userData.getUserId(), userData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateCrossUnionChestUserDataSql = "update `t_c_unionchest_user` set openNum=?,lastOpenTime=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(CrossUnionChestUserData crossUnionChestUserData) {
        crossUnionChestUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionChestUserData.getOpenNum()));
        para.put(i++, new DbParameter(Types.BIGINT, crossUnionChestUserData.getLastOpenTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionChestUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossUnionChestUserData.getUserId()));
        if (getDb().executeNoneQuery(updateCrossUnionChestUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossUnionChestUserDataSql = "insert into `t_c_unionchest_user`(activityId,userId,openNum,lastOpenTime) values (?,?,?,?)";

    @Override
    protected boolean addData(CrossUnionChestUserData crossUnionChestUserData) {
        crossUnionChestUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionChestUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossUnionChestUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossUnionChestUserData.getOpenNum()));
        para.put(i++, new DbParameter(Types.BIGINT, crossUnionChestUserData.getLastOpenTime()));
        if (getDb().executeNoneQuery(insertCrossUnionChestUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossUnionChestUserData parseData(ResultSet rs) throws SQLException {
        CrossUnionChestUserData crossUnionChestUserData = new CrossUnionChestUserData();
        crossUnionChestUserData.setActivityId(rs.getInt("activityId"));
        crossUnionChestUserData.setUserId(rs.getLong("userId"));
        crossUnionChestUserData.setOpenNum(rs.getInt("openNum"));
        crossUnionChestUserData.setLastOpenTime(rs.getLong("lastOpenTime"));

        return crossUnionChestUserData;
    }
}
